package com.mdd.admin.controller;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.ILegalAreaCityService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.LegalAreaCityCreateValidate;
import com.mdd.admin.validate.LegalAreaCityUpdateValidate;
import com.mdd.admin.validate.LegalAreaCitySearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalAreaCityListedVo;
import com.mdd.admin.vo.LegalAreaCityDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/city")
@Api(tags = "归属地管理")
public class LegalAreaCityController {

    @Resource
    ILegalAreaCityService iLegalAreaCityService;

    @GetMapping("/list")
    @ApiOperation(value="归属地列表")
    public AjaxResult<PageResult<LegalAreaCityListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated LegalAreaCitySearchValidate searchValidate) {
        PageResult<LegalAreaCityListedVo> list = iLegalAreaCityService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/all/province")
    @ApiOperation(value="省份列表")
    public AjaxResult<List<LegalAreaCityListedVo>> allProvince() {
        List<LegalAreaCityListedVo> list = iLegalAreaCityService.allProvinceOrCity(1);
        return AjaxResult.success(list);
    }

    @GetMapping("/all/city")
    @ApiOperation(value="省份列表")
    public AjaxResult<List<LegalAreaCityListedVo>> allCity() {
        List<LegalAreaCityListedVo> list = iLegalAreaCityService.allProvinceOrCity(2);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="归属地详情")
    public AjaxResult<LegalAreaCityDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        LegalAreaCityDetailVo detail = iLegalAreaCityService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "归属地新增")
    @PostMapping("/add")
    @ApiOperation(value="归属地新增")
    public AjaxResult<Object> add(@Validated @RequestBody LegalAreaCityCreateValidate createValidate) {
        iLegalAreaCityService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "归属地编辑")
    @PostMapping("/edit")
    @ApiOperation(value="归属地编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody LegalAreaCityUpdateValidate updateValidate) {
        iLegalAreaCityService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "归属地删除")
    @PostMapping("/del")
    @ApiOperation(value="归属地删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iLegalAreaCityService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
